package com.sunday.fangdaifu.activity.task;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.EmptyLayout;
import com.sunday.common.widgets.recyclerView.HorizontalDividerItemDecoration;
import com.sunday.fangdaifu.R;
import com.sunday.fangdaifu.adapter.CommonAdapter;
import com.sunday.fangdaifu.adapter.RecylerViewHolder;
import com.sunday.fangdaifu.common.Api;
import com.sunday.fangdaifu.common.ApiClient;
import com.sunday.fangdaifu.common.ApiServiceImpl;
import com.sunday.fangdaifu.model.ProblemType;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 刘涛 on 2017/7/19.
 */

public class ProblemClassifyListActivity extends BaseActivity implements ApiServiceImpl.HttpResponseInterface {

    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;
    @Bind(R.id.swipe_refresh_layout)
    SwipeRefreshLayout swipeRefreshLayout;
    @Bind(R.id.empty_layout)
    EmptyLayout emptyLayout;

    private int typeId;
    private int problemId, vhIndex;
    private String typeName;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recyclerlist);
        ButterKnife.bind(this);
        typeId = getIntent().getIntExtra("typeId", 0);
        vhIndex = getIntent().getIntExtra("vhIndex", 0);
        problemId = getIntent().getIntExtra("problemId", 0);
        typeName = getIntent().getStringExtra("typeName");
        titleView.setText("常见问题");
        initRecycleView();
        getData();

    }

    private CommonAdapter<ProblemType> adapter;
    private List<ProblemType> dataSet = new ArrayList<>();

    private void initRecycleView() {
        emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
        recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(mContext).drawable(R.drawable.shape_divider).build());
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new CommonAdapter<ProblemType>(mContext, R.layout.list_problem_item, dataSet) {
            @Override
            public void convert(RecylerViewHolder holder, final ProblemType problemType) {
                ((TextView) holder.getView(R.id.name)).setText(problemType.getName());
                holder.setOnClickListener(R.id.name, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (typeId != 0) {
                            Intent intent = new Intent(mContext, ProblemClassifyListActivity.class);
                            intent.putExtra("problemId", problemType.getId());
                            intent.putExtra("typeName", problemType.getName());
                            intent.putExtra("vhIndex", vhIndex);
                            startActivityForResult(intent, 0x1112);
                        } else if (problemId != 0) {
                            Intent data = new Intent();
                            data.putExtra("problem", problemType.getName());//具体问题
                            data.putExtra("problemType", typeName);//问题名称(分类)
                            data.putExtra("vhIndex", vhIndex);
                            setResult(RESULT_OK, data);
                            finish();
                        }
                    }
                });
            }
        };
        recyclerView.setAdapter(adapter);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData();
            }
        });
        emptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getData();
            }
        });

    }

    private void getData() {
        if (typeId != 0)
            ApiClient.getApiService().getPartsTypeList(typeId, this, new TypeToken<ResultDO<List<ProblemType>>>() {
            }.getType());
        else if (problemId != 0)
            ApiClient.getApiService().getProblemList(problemId, this, new TypeToken<ResultDO<List<ProblemType>>>() {
            }.getType());
    }

    @Override
    public void onResponce(String key, Object result) {
        if (isFinish) {
            return;
        }
        switch (key) {
            case Api.PARTS_TYPE_LIST:
                ResultDO<List<ProblemType>> resultDO = (ResultDO<List<ProblemType>>) result;
                swipeRefreshLayout.setRefreshing(false);
                if (resultDO.getCode() == 0) {
                    if (resultDO.getResult() != null)
                        dataSet.clear();
                    dataSet.addAll(resultDO.getResult());
                    emptyLayout.setErrorType(!dataSet.isEmpty() ? EmptyLayout.HIDE_LAYOUT : EmptyLayout.NODATA);
                    adapter.notifyDataSetChanged();
                } else {
                    ToastUtils.showToast(mContext, resultDO.getMessage());
                    emptyLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
                }
                break;
            case Api.PROBLEM_LIST:
                ResultDO<List<ProblemType>> resultDO1 = (ResultDO<List<ProblemType>>) result;
                swipeRefreshLayout.setRefreshing(false);
                if (resultDO1.getCode() == 0) {
                    if (resultDO1.getResult() != null)
                        dataSet.clear();
                    dataSet.addAll(resultDO1.getResult());
                    emptyLayout.setErrorType(!dataSet.isEmpty() ? EmptyLayout.HIDE_LAYOUT : EmptyLayout.NODATA);
                    adapter.notifyDataSetChanged();
                } else {
                    ToastUtils.showToast(mContext, resultDO1.getMessage());
                    emptyLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
                }
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        setResult(RESULT_OK, data);
        finish();
    }

    @Override
    public void onFailure() {
        swipeRefreshLayout.setRefreshing(false);
        ToastUtils.showToast(mContext, "网络连接错误");
    }
}
